package service

import (
	"shop/dao"
	"shop/entity"
	"shop/utils/result"
	"shop/utils/sessionutil"
)

// GetUserInfo 获取用户信息，has cache
func GetUserInfo(userId int64) *entity.User {
	user := sessionutil.GetUserInfoFromCache(userId)
	if user.Id == userId {
		return user
	}
	dao.GetDB().Model(user).Where("id", userId).Take(user)
	sessionutil.SetUserInfoCache(user)
	return user
}

func UpdateUser(user *entity.UserFilter, userId int64) *result.Result {
	err := dao.GetTx().Model(user).Where("id", userId).Updates(user).Commit().Error
	if err != nil {
		return result.Fail(err.Error())
	}
	sessionutil.Logout(userId)
	sessionutil.SetUserInfoCache(GetUserInfo(userId))
	return result.Ok(nil)
}
